<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%
	Integer  uid = token.getUid();
	Integer pageNum = input.getInt("page", 1);; // 页码
	Integer itemCount = input.getInt("item_count", 20); // 每一页条目
	if (pageNum <= 0 || itemCount <= 0) {
		Tools.encryptPrint(out, new Message(30041, "参数不合法，请输入正确的页码或限值"));
		return;
	}
	String date = input.getDesc("date", DateUtil.formatDate(new Date()));
	Where where = new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("step_time=", date);
	List<Map> ranks = QBuilder.db("basedb").select("new map(id as id, step as step, uid as uid)").from("steps").where(where).page(itemCount, pageNum).order("step", "desc").query().getResultObj();
	List<Long> rankCount = QBuilder.db("basedb").select("count(*)").from("steps").where(where).query().getResultObj();
	org.hibernate.StatelessSession ss = DBTools.db("basedb").getSf().openStatelessSession();
	List<Map> selfStepList = ss.createSQLQuery(String.format("select * from (SELECT A.*,@rank\\:=@rank+1 AS pm FROM (SELECT id as id, step as step, uid as uid FROM steps where step_time='%s' and hospital=%s ORDER BY step desc) A, (SELECT  @rank\\:=0) B ) as c where c.uid=%s", new Object[]{date, hospital, uid})).setResultTransformer(org.hibernate.transform.Transformers.ALIAS_TO_ENTITY_MAP).list();
	if (ss != null)
		ss.close();
	List<Map> championStepList = QBuilder.db("basedb").select("new map(id as id, step as step, uid as uid)").from("steps").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("step_time=", date)).page(1, 1).order("step", "desc").query().getResultObj();
	List<Integer> uids = new ArrayList<Integer>();
	for (Map map:ranks) {
		uids.add((Integer)map.get("uid"));
	}
	uids.add(token.getUid());
	Map<Integer, User> userMap = usersByUids(uids);
	User self = userMap.get(token.getUid()) == null?new User():userMap.get(token.getUid());
	Map selfMap = isEmpty(selfStepList)?new HashMap():selfStepList.get(0);
	selfMap.put("name", getDisplayName(self));
	selfMap.put("avatar", getDefaultAvatar(self));
	selfMap.put("gender", self.getGender());
	selfMap.put("background", self.getJson("background"));
	selfMap.put("order", selfMap.remove("pm"));
	Map championMap = isEmpty(championStepList)?new HashMap():championStepList.get(0);
	User champion = userMap.get((Integer)championMap.get("uid")) == null?new User():userMap.get((Integer)championMap.get("uid"));
	championMap.put("name", getDisplayName(champion));
	championMap.put("avatar", getDefaultAvatar(champion));
	championMap.put("gender", champion.getGender());
	championMap.put("order",  1);
	championMap.put("background", champion.getJson("background"));
	Map resultMap = new HashMap();
	completeOperateCount(selfMap, STEP_CATEGORY, hospital,  uid);
	completeOperateCount(championMap, STEP_CATEGORY, hospital,  uid);
	resultMap.put("self", selfMap);
	resultMap.put("champion", championMap);
	Long size =  isEmpty(rankCount)?0L:rankCount.get(0);
	for (int i = 0; i < ranks.size();i++) {
		Map map = ranks.get(i);
		map.put("order", (pageNum -1)*itemCount + i+ 1);
		Integer id = (Integer)map.get("uid");
		User user = userMap.get(id);
		if (user != null) {
			map.put("name", getDisplayName(user));
			map.put("avatar", getDefaultAvatar(user));
			map.put("gender", 1);
			map.put("background", user.getJson("background"));
		}
		if (user == null) {
			map.put("name", "未知用户");
			map.put("avatar", getDefaultAvatar(new User()));
			map.put("gender", 1);
			map.put("background", null);
		}
	}
	completeOperateCount(ranks, STEP_CATEGORY, hospital,  uid);
	resultMap.put("rank", ranks);
	resultMap.put("current_page", pageNum);
	resultMap.put("page_count",  size%itemCount == 0?(size/itemCount):(size/itemCount + 1));
	resultMap.put("item_count",  itemCount);
	msg.put("data", resultMap);
	Tools.encryptPrint(out, msg.toJson());
%>
